package com.demo.travel.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.demo.travel.R;
import com.demo.travel.activity.NewsDetailActivity;
import com.demo.travel.adapter.NewsAdapter;
import com.demo.travel.base.BaseFragment;
import com.demo.travel.bean.ItemBean;
import com.j256.ormlite.stmt.query.In;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 *
 * Create time 2021/6/15 9:40
 * 新闻
 */
public class NewsFragment extends BaseFragment {

    @BindView(R.id.mBanner)
    Banner mBanner;
    @BindView(R.id.title1Tv)
    TextView title1Tv;
    @BindView(R.id.imageView1)
    ImageView imageView1;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.imageView3)
    ImageView imageView3;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    @BindView(R.id.textView1)
    TextView textView1;
    @BindView(R.id.textView2)
    TextView textView2;
    @BindView(R.id.textView3)
    TextView textView3;

    private NewsAdapter newsAdapter;

    private ItemBean itemBean1,itemBean2, itemBean3;

    private ItemBean currentBean;

    @Override
    public int getLayout() {
        return R.layout.fragment_news;
    }

    @Override
    public String getTitle() {
        return "新闻";
    }

    @Override
    public void initData() {
        initBanner();
        newsAdapter = new NewsAdapter();
        recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerview.setAdapter(newsAdapter);

        newsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ItemBean itemBean = newsAdapter.getData().get(position);
                Intent intent = new Intent(getActivity(), NewsDetailActivity.class);
                intent.putExtra("itemBean", itemBean);
                getActivity().startActivity(intent);
            }
        });

        itemBean1 = new ItemBean(getStringText(R.string.news1_title), R.mipmap.news1_1);
        itemBean1.setContent(getStringText(R.string.news1_content));
        itemBean2 = new ItemBean(getStringText(R.string.news2_title), R.mipmap.news2_1);
        itemBean2.setContent(getStringText(R.string.news2_content));
        itemBean3 = new ItemBean(getStringText(R.string.news3_title), R.mipmap.news3_1);
        itemBean3.setContent(getStringText(R.string.news3_content));

        newsAdapter.addData(itemBean1);
        newsAdapter.addData(itemBean2);
        newsAdapter.addData(itemBean3);

        title1Tv.setText(getStringText(R.string.news3_title));
        imageView1.setImageResource(R.mipmap.news3_1);
        imageView2.setImageResource(R.mipmap.news3_2);
        imageView3.setImageResource(R.mipmap.news3_3);

        currentBean = itemBean3;

    }

    @Override
    public void requestData() {

    }

    private void initBanner() {
        int[] imageResourceID = new int[]{R.mipmap.news1_1, R.mipmap.news2_1, R.mipmap.riluo};
        List<Integer> imgeList = new ArrayList<>();
        //轮播标题
        String[] mtitle = new String[]{getStringText(R.string.news1_title),getStringText(R.string.news2_title),getStringText(R.string.news3_title)};
        List<String> titleList = new ArrayList<>();

        for (int i = 0; i < imageResourceID.length; i++) {
            imgeList.add(imageResourceID[i]);//把图片资源循环放入list里面
            titleList.add(mtitle[i]);//把标题循环设置进列表里面
            //设置图片加载器，通过Glide加载图片
            mBanner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    Glide.with(getActivity()).load(path).into(imageView);
                }
            });
            //设置轮播的动画效果,里面有很多种特效,可以到GitHub上查看文档。
            mBanner.setBannerAnimation(Transformer.Accordion);
            mBanner.setImages(imgeList);//设置图片资源
            mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);//设置banner显示样式（带标题的样式）
            mBanner.setBannerTitles(titleList); //设置标题集合（当banner样式有显示title时）
            //设置指示器位置（即图片下面的那个小圆点）
            mBanner.setIndicatorGravity(BannerConfig.CENTER);
            mBanner.setDelayTime(3000);//设置轮播时间3秒切换下一图
//            mBanner.setOnBannerListener(this);//设置监听
            mBanner.start();//开始进行banner渲染
        }
    }

    @OnClick({R.id.layout1, R.id.layout2, R.id.layout3,R.id.imageView1,R.id.imageView2,R.id.imageView3,R.id.title1Tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout1:
                textView1.setTextColor(Color.parseColor("#1296db"));
                textView2.setTextColor(Color.parseColor("#2E2E2E"));
                textView3.setTextColor(Color.parseColor("#2E2E2E"));
                Drawable drawable = mContext.getResources().getDrawable(R.mipmap.hot_s);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                textView1.setCompoundDrawables( drawable, null, null,null);
                Drawable drawable1 = mContext.getResources().getDrawable(R.mipmap.location);
                drawable1.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                textView2.setCompoundDrawables(drawable1,null, null,  null);
                Drawable drawable2 = mContext.getResources().getDrawable(R.mipmap.zhuanti);
                drawable2.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                textView3.setCompoundDrawables(drawable2, null, null, null);
                title1Tv.setText(getStringText(R.string.news3_title));
                imageView1.setImageResource(R.mipmap.news3_1);
                imageView2.setImageResource(R.mipmap.news3_2);
                imageView3.setImageResource(R.mipmap.news3_3);

                newsAdapter.getData().clear();
                newsAdapter.addData(itemBean1);
                newsAdapter.addData(itemBean2);
                newsAdapter.addData(itemBean3);

                currentBean = itemBean3;

                break;
            case R.id.layout2:
                textView2.setTextColor(Color.parseColor("#1296db"));
                textView1.setTextColor(Color.parseColor("#2E2E2E"));
                textView3.setTextColor(Color.parseColor("#2E2E2E"));
                Drawable drawable3 = mContext.getResources().getDrawable(R.mipmap.hot);
                drawable3.setBounds(0, 0, drawable3.getMinimumWidth(), drawable3.getMinimumHeight());
                textView1.setCompoundDrawables(drawable3, null, null, null);
                Drawable drawable4 = mContext.getResources().getDrawable(R.mipmap.bendi_s);
                drawable4.setBounds(0, 0, drawable4.getMinimumWidth(), drawable4.getMinimumHeight());
                textView2.setCompoundDrawables(drawable4,null, null,  null);
                Drawable drawable5 = mContext.getResources().getDrawable(R.mipmap.zhuanti);
                drawable5.setBounds(0, 0, drawable5.getMinimumWidth(), drawable5.getMinimumHeight());
                textView3.setCompoundDrawables(drawable5, null, null, null);
                title1Tv.setText(getStringText(R.string.news2_title));
                imageView1.setImageResource(R.mipmap.news2_1);
                imageView2.setImageResource(R.mipmap.news2_2);
                imageView3.setImageResource(R.mipmap.news2_3);

                newsAdapter.getData().clear();
                newsAdapter.addData(itemBean3);
                newsAdapter.addData(itemBean2);
                newsAdapter.addData(itemBean1);

                currentBean = itemBean2;
                break;
            case R.id.layout3:
                textView3.setTextColor(Color.parseColor("#1296db"));
                textView2.setTextColor(Color.parseColor("#2E2E2E"));
                textView1.setTextColor(Color.parseColor("#2E2E2E"));
                Drawable drawable6 = mContext.getResources().getDrawable(R.mipmap.hot);
                drawable6.setBounds(0, 0, drawable6.getMinimumWidth(), drawable6.getMinimumHeight());
                textView1.setCompoundDrawables(drawable6, null, null, null);
                Drawable drawable7 = mContext.getResources().getDrawable(R.mipmap.location);
                drawable7.setBounds(0, 0, drawable7.getMinimumWidth(), drawable7.getMinimumHeight());
                textView2.setCompoundDrawables(drawable7, null, null, null);
                Drawable drawable8 = mContext.getResources().getDrawable(R.mipmap.zhuanti_s);
                drawable8.setBounds(0, 0, drawable8.getMinimumWidth(), drawable8.getMinimumHeight());
                textView3.setCompoundDrawables(drawable8,null, null, null);
                title1Tv.setText(getStringText(R.string.news1_title));
                imageView1.setImageResource(R.mipmap.news1_1);
                imageView2.setImageResource(R.mipmap.news1_2);
                imageView3.setImageResource(R.mipmap.news1_3);

                newsAdapter.getData().clear();
                newsAdapter.addData(itemBean2);
                newsAdapter.addData(itemBean1);
                newsAdapter.addData(itemBean3);

                currentBean = itemBean1;
                break;
            case R.id.imageView1:
            case R.id.imageView3:
            case R.id.imageView2:
            case R.id.title1Tv:
                goActivity();
                break;
        }
    }

    private void goActivity(){
        Intent intent = new Intent(getActivity(), NewsDetailActivity.class);
        intent.putExtra("itemBean", currentBean);
        getActivity().startActivity(intent);
    }
}
